﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using com.woodsoft.core;
using com.woodsoft.core.Files;
using EQ1.Elements;
using EQ1.Objects;

namespace EQ1.Elements {
  public class Frame : UIClass , IUIElement<Frame> {
    public Frame ( UIDocument doc , string name )
      : base( doc , name ) {
      this.Texture = "";
      this.Duration = 1000;
    }
    public Frame ( UIDocument doc )
      : this( doc , "Frame" ) {
    }

    public string Texture {
      get {
        return this.GetElement( "Texture" ).InnerText;
      }
      set {
        if( this.GetElement( "Texture" ) == null )
          this.AddElement( "Texture" , value );
        else
          this.GetElement( "Texture" ).InnerText = value;
      }
    }
    public UIPoint Location {
      get {
        return ( UIPoint ) this.GetElement( "Location" );
      }
      set {
        this.Location.Validate( value , "Location" );
      }
    }
    public UISize Size {
      get {
        return ( UISize ) this.GetElement( "Size" );
      }
      set {
        this.Size.Validate( value , "Size" );
      }
    }
    public UIPoint Hotspot {
      get {
        return ( UIPoint ) this.GetElement( "Hotspot" );
      }
      set {
        this.Hotspot.Validate( value , "Hotspot" );
      }
    }
    public int Duration {
      get {
        return int.Parse( this.GetElement( "Duration" ).InnerText );
      }
      set {
        this.Validate( "Duration" , value.ToString() );
      }
    }
    public XmlNodeList Shading {
      get {
        return this.GetElementsByTagName( "Shading" );
      }
      set {
        if( value.Count != 0 ) {
          foreach( RGB nd in value ) {
            RGB shad = new RGB( ( UIDocument ) nd.OwnerDocument , "Shading" );
            shad.R = nd.R;
            shad.G = nd.G;
            shad.B = nd.B;
            shad.Alpha = nd.Alpha;
            this.AddShading( shad );
          }
        }
      }
    }
    public XmlNodeList Specular {
      get {
        return this.GetElementsByTagName( "Specular" );
      }
      set {
        if( value.Count != 0 ) {
          foreach( RGB nd in value ) {
            RGB spec = new RGB( ( UIDocument ) nd.OwnerDocument , "Specular" );
            spec.R = nd.R;
            spec.G = nd.G;
            spec.B = nd.B;
            spec.Alpha = nd.Alpha;
            this.AddSpecular( spec );
          }
        }
      }
    }
    public RGB GetShading ( string item ) {
      foreach( RGB nd in this.Shading ) {
        if( nd.item == item )
          return nd;
      }
      return null;
    }
    public RGB GetSpecular ( string item ) {
      foreach( RGB nd in this.Specular ) {
        if( nd.item == item )
          return nd;
      }
      return null;
    }
    public void AddShading ( RGB shading ) {
      if( shading.LocalName != "Shading" ) {
        RGB el = new RGB( ( UIDocument ) shading.OwnerDocument , "Shading" );
        el.Alpha = shading.Alpha;
        el.R = shading.R;
        el.G = shading.G;
        el.B = shading.B;

        shading = el;
      }

      for( int i = this.Shading.Count - 1 ; i >= 0 ; i-- ) {
        if( ( ( RGB ) this.Shading[i] ).item == shading.item ) {
          this.ReplaceChild( shading , this.Shading[i] );
          return;
        }
      }

      this.AppendChild( shading );
    }
    public void AddSpecular ( RGB specular ) {
      if( specular.LocalName != "Specular" ) {
        RGB el = new RGB( ( UIDocument ) specular.OwnerDocument , "Specular" );
        el.R = specular.R;
        el.G = specular.G;
        el.B = specular.B;
        el.Alpha = specular.Alpha;

        specular = el;
      }

      for( int i = this.Specular.Count - 1 ; i >= 0 ; i-- ) {
        if( ( ( RGB ) this.Specular[i] ).item == specular.item ) {
          this.ReplaceChild( specular , this.Specular[i] );
          return;
        }
      }

      this.AppendChild( specular );
    }

    #region IUIElement<Frame> Members

    public void Copy ( Frame val ) {
      base.Copy( val );

      this.Duration = val.Duration;
      this.Texture = val.Texture;

      if( val.Location != null )
        this.Location = val.Location;
      if( val.Size != null )
        this.Size = val.Size;
      if( val.Hotspot != null )
        this.Hotspot = val.Hotspot;
      if( val.Shading != null )
        this.Shading = val.Shading;
      if( val.Specular != null )
        this.Specular = val.Specular;
    }
    public void Validate ( Frame val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        Frame el = new Frame( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }

    #endregion
  }
}
